package com.itany.ums.servlet;

import com.itany.ums.entity.User;
import com.itany.ums.exception.RequestParameterErrorException;
import com.itany.ums.exception.UserExistException;
import com.itany.ums.exception.UserNotExistException;
import com.itany.ums.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/10 9:33
 * Version:1.0-SNAPSHOT
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {

    private UserService userService;

    @Override
    public void init() throws ServletException {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//        WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
        userService = (UserService) ac.getBean("userServiceImpl");
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        if("/register.user".equals(path)){
            register(request,response);
        }
        if("/login.user".equals(path)){
            login(request,response);
        }
        if("/logout.user".equals(path)){
            logout(request,response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        request.getSession().invalidate();
        response.sendRedirect(request.getContextPath()+"/main.jsp");
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        try {
            User user = userService.login(username,password);
            request.getSession().setAttribute("user",user);
            response.sendRedirect(request.getContextPath()+"/main.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");

        try {
            userService.register(username,password,phone,address);
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        } catch (RequestParameterErrorException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        } catch (UserExistException e) {
            request.setAttribute("registerMsg",e.getMessage());
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registerMsg","服务器内部异常");
            request.getRequestDispatcher("/register.jsp").forward(request,response);
        }

    }


}
